<?php
/*
Chương trình: "Quản lý Hóa Đơn và in hóa đơn Trường Thành"
Tác giả: "Lê Phương"
Phiên bản: "1.0"
Tệp tin: "manage.php"
*/
define("template",true);
$warning = '';
$id = $_REQUEST['id'];
if(isset($_REQUEST['edit']))
{
	$edit = $_REQUEST['edit'];
	$edit_query1 = mysql_query("SELECT * FROM `product` WHERE id='$edit'");
	$edit_pr = mysql_fetch_object($edit_query1);
	$edit_query2 = mysql_query("SELECT * FROM `contract` WHERE `id` = '$edit_pr->cid'");
	$edit_rs = mysql_fetch_object($edit_query2);
	$name = $edit_rs->name; 
	$address = $edit_rs->address;
	$tax = $edit_rs->tax;
	$form = $edit_pr->form;
	$serial = $edit_pr->serial;
	include('template/edit.tpl.php');
}
else
{
	if(isset($_GET['page'])=="")
		{
			$page =1;
		}
		else
		{
			$page = $_GET['page'];
		}
	$SQL = "select count(*) from contract";
	$Result=mysql_query($SQL);
	$total = @mysql_result($Result, 0, 0);  
	$limit = 20;
	$pager  = Pager::getPagerData($total, $limit, $page);
	$offset = $pager->offset;
	$limit  = $pager->limit;
	$page   = $pager->page;
	if($offset>=0)
	{
		if(isset($_GET["submit3"]))
		{
			$search = $_GET['search'];
			$res_query =mysql_query("select * from contract where name like '%$search%'");
		}
		else
		{
			$res_query =mysql_query("select * from contract order by date desc limit $offset, $limit");
		}
	}
	else
	{
		$res_query =mysql_query("select * from contract order by date desc");
	}
	echo mysql_error();
	$numrows = mysql_num_rows($res_query);
	$iCnt=0;
	/*bắt đầu hiển thị vòng lặp
	show.inc.php
	kết thúc lặp*/
	include ('template/manage.tpl.php');
}
class Pager
{
	function getPagerData($numHits, $limit, $page)
	{
		$numHits  = (int) $numHits;
		$limit    = max((int) $limit, 1);
		$page     = (int) $page;
		$numPages = ceil($numHits / $limit);
		$page = max($page, 1);
		$page = min($page, $numPages);
		$offset = ($page - 1) * $limit;
		$ret = new stdClass;
		$ret->offset   = $offset;
		$ret->limit    = $limit;
		$ret->numPages = $numPages;
		$ret->page     = $page;
		return $ret;
	}
}
?>